
###################################################
##名称：预制装配式楼梯sketchup ruby api类封装代码
##作者：钢之炼筋术士
##日期：2023.8.4 完成
##测试软件：sketchup pro 2020  
##测试版本：20.0.363 64位
##参考文档：ruby.sketchup.com;www.runoob.com/ruby/
##################################################

class PcStairs

    def initialize(stepWidth = 280, stepHeight = 160,               #构造函数
        stepNum = 18 , platformTopLength = 1520,
        platformBottomLength = 400 , stairBoardHeight = 270,
        stairBoardWidth = 2450)

        #####构造对应参数######

        @stepWidth = stepWidth.to_f.mm                                         #踏步宽
        @stepHeight = stepHeight.to_f.mm                                       #踏步高
        @stepNum = stepNum.to_i                                           #踏步数
        @platformTopLength = platformTopLength.to_f.mm                         #上平台长
        @platformBottomLength = platformBottomLength.to_f.mm                   #下平台长
        @stairBoardHeight = stairBoardHeight.to_f.mm                           #梯板高
        @stairBoardWidth = stairBoardWidth.to_f.mm                             #梯板宽

        #####换算参数########

        @totalHeight = @stairBoardHeight + @stepHeight * @stepNum                                 #总高
        @totalLength = @platformTopLength + @stepWidth * (@stepNum - 1) + @platformBottomLength   #总长

        #####1/2正切偏移值#######

        @ptfbpy = Math.tan(Math.atan(160.0/280.0)/2)*@stairBoardHeight + @platformBottomLength    #下平台偏移出
        @ptftpy = @platformTopLength - Math.tan(Math.atan(160.0/280.0)/2)*@stairBoardHeight       #上平台偏移入

        #####提量用系数########

        #用于mm为模型单位的文件
        @areaXs = 1550.0031000062013                  #面积系数
        @volumeXs = 61023.744094732334                #体积系数

        ####群组结构######

        @group = Sketchup.active_model.entities.add_group      #最外层群组

    end

    def drawPcStairs                   #画楼梯模型

        pts = []
        pts.push([0, 0, @totalHeight])
        pts.push([@platformTopLength, 0, @totalHeight])
        
        i = 1
        
        while i < @stepNum  do
            
           pts.push([@platformTopLength + @stepWidth * (i-1), 0, @totalHeight - @stepHeight * i])
           pts.push([@platformTopLength + @stepWidth * i, 0, @totalHeight - @stepHeight * i])
           i +=1
        
        end
        
        pts.push([@platformTopLength + @stepWidth * (@stepNum - 1), 0, @totalHeight - @stepHeight * @stepNum])
        pts.push([@totalLength, 0, @totalHeight - @stepHeight * @stepNum])
        
        
        pts.push([@totalLength, 0, 0])
        pts.push([@totalLength - @ptfbpy, 0, 0])
        pts.push([@ptftpy - @stepWidth, 0, @totalHeight - @stairBoardHeight])
        
        #pts.push([platformTopLength, 0, 0])
        pts.push([0, 0, @totalHeight - @stairBoardHeight])
        
        
        face = @group.entities.add_face(pts)
        face.reverse!
        
        face.pushpull(@stairBoardWidth, true)
        

    end

    def getVolume                      #获取体积
    
        @group.volume/@volumeXs

    end

    def getArea                        #获取面积
    
        @group.entities.each{|e|

            if e.class == Sketchup::Face
              puts e.area/@areaXs
              puts e.normal
            end
        }    
    
    end

    def self.create_PcStairs(params)

        #@group.entities.add_circle([0,0,0], [0,0,1], 10) 
        pcSt = PcStairs.new(params[0],params[1],params[2],params[3],params[4],params[5],params[6])
        #sl = Sleeve.new()
        p params
        
        pcSt.drawPcStairs
        pcSt.getVolume
        pcSt.getArea
        
        return pcSt


    end

    def self.create_Dialog

        dialog = UI::HtmlDialog.new(
            {
              :dialog_title => "创建预制装配式楼梯类",
              :preferences_key => "",
              :scrollable => true,
              :resizable => true,
              :width => 800,
              :height => 400,
              :left => 100,
              :top => 100,
              :min_width => 50,
              :min_height => 50,
              :max_width =>1000,
              :max_height => 1000,
              :style => UI::HtmlDialog::STYLE_DIALOG
            })
            dialog.set_file File.join(File.dirname(__FILE__), 'htmldialog/pcStairs.html')
            dialog.show
            dialog.add_action_callback("confirm") { |action, params|
                # params即js传过来的值
                create_PcStairs(params)
            }

            dialog.add_action_callback("cancel"){
                dialog.close
            }


    end

end

#pcSt = PcStairs.create_PcStairs(['280','160','18','1520','400','270','2450'])





